package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ArchiveReceiveStat;
import com.ruoyi.system.domain.ArchiveUsageStats;
import com.ruoyi.system.service.ArchiveReceiveStatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 档案接收统计 Controller
 *
 * @author 你的名字
 */
@RestController
@RequestMapping("/system/archiveReceiveStat")
public class ArchiveReceiveStatController {
    @Autowired
    private ArchiveReceiveStatService archiveReceiveStatService;

    /**
     * 查询档案接收统计列表
     */
    @GetMapping("/list")
    public AjaxResult list(ArchiveReceiveStat archiveReceiveStat) {
        List<ArchiveReceiveStat> list = archiveReceiveStatService.selectArchiveReceiveStatList(archiveReceiveStat);
        List<String> names = new ArrayList<>();
        List<String> years = new ArrayList<>();
        List<Integer> zhiDatas = new ArrayList<>();
        List<Integer> numDatas = new ArrayList<>();
        List<Integer> numCopyDatas = new ArrayList<>();
        if(list != null && list.size() > 0)
        {
            names = list.stream().map(m -> m.getName()).collect(Collectors.toList());
            years = list.stream().map(m -> m.getYear()+"年").distinct().collect(Collectors.toList());
        }
        for (String year : years) {
            List<ArchiveReceiveStat> collect = list.stream().filter(f -> (f.getYear() + "年").equals(year)).collect(Collectors.toList());
            if(collect != null && collect.size() > 0)
            {
                for (ArchiveReceiveStat stat : collect) {
                    if(stat.getName().equals("纸质档案"))
                    {
                        if(stat.getValue() != null)
                        {
                            zhiDatas.add(stat.getValue());
                        }else{
                            zhiDatas.add(0);
                        }
                    }
                    if(stat.getName().equals("数字化副本（卷）"))
                    {
                        if(stat.getValue() != null)
                        {
                            numDatas.add(stat.getValue());
                        }else{
                            numDatas.add(0);
                        }
                    }
                    if(stat.getName().equals("数字化副本（件）"))
                    {
                        if(stat.getValue() != null)
                        {
                            numCopyDatas.add(stat.getValue());
                        }else{
                            numCopyDatas.add(0);
                        }
                    }
                }
            }

        }

        Map<String,Object> map = new HashMap<>();
        map.put("seriesData",list);
        map.put("xData",years);
        //年
        map.put("zhiData",zhiDatas);
        //纸质档案
        map.put("numData",numDatas);
        //数字化副本（卷）
        map.put("numCopyData",numCopyDatas);
        //数字化副本（件）
        map.put("lData",names);
        return AjaxResult.success(map); // 封装分页结果
    }

    /**
     * 获取档案接收统计详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(archiveReceiveStatService.selectArchiveReceiveStatById(id));
    }

    /**
     * 新增档案接收统计
     */
    @PostMapping
    public AjaxResult add(@RequestBody ArchiveReceiveStat archiveReceiveStat) {
        return AjaxResult.success(archiveReceiveStatService.insertArchiveReceiveStat(archiveReceiveStat));
    }

    /**
     * 修改档案接收统计
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ArchiveReceiveStat archiveReceiveStat) {
        return AjaxResult.success(archiveReceiveStatService.updateArchiveReceiveStat(archiveReceiveStat));
    }


    /**
     * 批量保存档案接收统计（先删后插）
     */
    @PostMapping("/batchSave")
    public AjaxResult batchSave(@RequestBody List<ArchiveReceiveStat> list) {
        return AjaxResult.success(archiveReceiveStatService.batchSaveArchiveReceiveStat(list));
    }
}